package com.czl.logDemon;

import java.io.InputStream;
import java.util.Map.Entry;
import java.util.Properties;

import org.apache.log4j.Logger;
import org.eclipse.jetty.server.Connector;
import org.eclipse.jetty.server.Handler;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.ServerConnector;
import org.eclipse.jetty.server.handler.ContextHandlerCollection;
import org.eclipse.jetty.server.handler.DefaultHandler;
import org.eclipse.jetty.servlet.ServletContextHandler;

public class BootStrap {

	private static Logger logger = Logger.getLogger(BootStrap.class);
	
	public static void main(String[] args) {
		
		try {
			Properties app = new Properties();
			InputStream inStream = Thread.currentThread()
							.getContextClassLoader()
							.getResourceAsStream("application.properties");
			app.load(inStream);
			
			Properties boot = new Properties();
			InputStream inStream2 = Thread.currentThread()
								.getContextClassLoader()
								.getResourceAsStream("bootstrap.properties");
			boot.load(inStream2);
			
			init(app,boot);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		
	}

	private static void init(Properties app,Properties boot) throws Exception{
		
		Server server = new Server();//初始化服务
		ServerConnector connect = new ServerConnector(server);//初始化链接
		connect.setPort(Integer.valueOf(boot.getProperty("port","9999")));
		Connector[] connectors = {connect};
		
		server.setConnectors(connectors);//设置连接
		ContextHandlerCollection handler = new ContextHandlerCollection();
		ServletContextHandler servletContextHandler = new ServletContextHandler();
		servletContextHandler.setContextPath(boot.getProperty("context.path","/"));
		for(Entry<Object,Object> entry:app.entrySet()){
			servletContextHandler.addServlet(entry.getValue().toString()
					,entry.getKey().toString().trim());
		}		
		Handler[] handlers = {servletContextHandler,new DefaultHandler()};
		handler.setHandlers(handlers);
		server.setHandler(handler);//设置处理器
		server.start();//开启服务
		server.join();
		
	}
	
	

}


